package com.moorecat.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.bean.Shop;
import com.moorecat.http.HttpUtils;
import com.moorecat.utils.UtilTools;
import com.moorecat.utils.ui.UiUtils;

public class Comment extends Activity implements OnClickListener {

	private static final String TAG = "Comment";

	private EditText people, avg, title, remark;

	private RatingBar ratingBar;

	private TextView taste, envrionment, service;

	private AlertDialog dialog;

	private ImageView submit;

	private MooerApplication mooerApp;

	private CharSequence[] items = new CharSequence[] { "非常好", "很好", "好", "一般",
			"差" };

	private int tasteItem, envrionmentItem, serviceItem;

	private int shopId;

	private boolean submitting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.comment);
		Intent intent = getIntent();
		shopId = intent.getIntExtra("shopId", 0);
		mooerApp = (MooerApplication) getApplication();
		ratingBar = (RatingBar) findViewById(R.id.ratingbar);
		people = (EditText) findViewById(R.id.edit_people);
		avg = (EditText) findViewById(R.id.edit_avg);
		title = (EditText) findViewById(R.id.edit_title);
		remark = (EditText) findViewById(R.id.edit_remark);
		taste = (TextView) findViewById(R.id.select_taste);
		envrionment = (TextView) findViewById(R.id.select_env);
		service = (TextView) findViewById(R.id.select_service);
		submit = (ImageView) findViewById(R.id.submit);

		taste.setOnClickListener(this);
		envrionment.setOnClickListener(this);
		service.setOnClickListener(this);
		submit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == taste) {
			showAlertDialog("口味", tasteItem);
		} else if (v == envrionment) {
			showAlertDialog("环境", envrionmentItem);
		} else if (v == service) {
			showAlertDialog("服务", serviceItem);
		} else if (v == submit) {
			if (submitting) {
				UiUtils.showToast(this, "正在提交评论，请稍候再进行评论");
				return;
			}
			submitting = true;
			com.moorecat.bean.Comment comment = new com.moorecat.bean.Comment();
			if (ratingBar.getRating() == 0) {
				UiUtils.showToast(this, "请进行综合评分");
				return;
			} else {
				comment.setRating(ratingBar.getRating());
			}
			if (people.getText().toString().length() > 0) {
				comment.setPeople(Integer.parseInt(people.getText().toString()));
			} else {
				UiUtils.showToast(this, "请填写人数");
				return;
			}
			if (avg.getText().toString().length() > 0) {
				comment.setCost(Float.parseFloat(avg.getText().toString()));
			} else {
				UiUtils.showToast(this, "请填写平均消费");
				return;
			}
			if (title.getText().toString().length() > 0) {
				comment.setTitle(title.getText().toString());
			} else {
				UiUtils.showToast(this, "请填写标题");
			}
			if (remark.getText().toString().length() > 0) {
				comment.setContent(remark.getText().toString());
			} else {
				UiUtils.showToast(this, "请填写内容");
				return;
			}
			comment.setRating_taste(5 - tasteItem);
			comment.setRating_env(5 - envrionmentItem);
			comment.setRating_service(5 - serviceItem);
			comment.setUser(mooerApp.getUser());
			Shop shop = new Shop();
			shop.setId(shopId);
			comment.setShop(shop);
			new CommentTask().execute(comment);
		}

	}

	private void showAlertDialog(final String title, int selected) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setSingleChoiceItems(items, selected,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						if ("口味".equals(title)) {
							taste.setText(items[item]);
							tasteItem = item;
						} else if ("环境".equals(title)) {
							envrionment.setText(items[item]);
							envrionmentItem = item;
						} else if ("服务".equals(title)) {
							service.setText(items[item]);
							serviceItem = item;
						}
						dialog.dismiss();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	private class CommentTask extends
			AsyncTask<com.moorecat.bean.Comment, Integer, String> {

		private boolean isException;

		@Override
		protected String doInBackground(com.moorecat.bean.Comment... params) {
			try {
				HttpUtils.doCommentShop(params[0]);
			} catch (Exception e) {
				isException = true;
				Log.e(TAG, "", e);
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			submitting = false;
			if (isException) {
				UiUtils.showToast(Comment.this, "评论失败");
			} else {
				UiUtils.showToast(Comment.this, "评论成功");
				com.moorecat.bean.Comment comment = new com.moorecat.bean.Comment();
				Shop shop = new Shop();
				comment.setShop(shop);
				comment.setUser(mooerApp.getUser());
				comment.setTitle(title.getText().toString());
				comment.setContent(remark.getText().toString());
				comment.setDate(UtilTools.obtainCurrentDate());
				comment.setRating(ratingBar.getRating());
				comment.setRating_taste(5 - tasteItem);
				comment.setRating_env(5 - envrionmentItem);
				comment.setRating_service(5 - serviceItem);
				comment.setPeople(Integer.parseInt(people.getText().toString()));
				comment.setCost(Float.parseFloat(avg.getText().toString()));
				Intent intent = new Intent();
				intent.putExtra("comment", comment);
				setResult(0, intent);
				finish();
			}
		}

	}
}
